package jys.test.yddata.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

import androidx.constraintlayout.widget.ConstraintLayout;

public class SquareRelativeLayout extends RelativeLayout {
    public SquareRelativeLayout(Context context) {
        super(context);
    }

    public SquareRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SquareRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setWH(int windows_w, int windows_h, int w, int h) {
        ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) this.getLayoutParams();
        params.width = windows_w;
        params.height = windows_h;
        this.setLayoutParams(params);

        int pre_w, pre_h;
        if (1.0*windows_w/windows_h<1.0*w/h) {
            pre_w = windows_w;
            pre_h = windows_w*h/w;
        } else {
            pre_w = windows_h*w/h;
            pre_h = windows_h;
        }

        RelativeLayout.LayoutParams childParams;
        childParams = (RelativeLayout.LayoutParams) this.getChildAt(0).getLayoutParams();
        childParams.width = pre_w;
        childParams.height = pre_h;
        this.getChildAt(0).setLayoutParams(childParams);

        childParams = (RelativeLayout.LayoutParams) this.getChildAt(1).getLayoutParams();
        childParams.width = pre_w;
        childParams.height = pre_h;
        this.getChildAt(0).setLayoutParams(childParams);

        childParams = (RelativeLayout.LayoutParams) this.getChildAt(2).getLayoutParams();
        childParams.width = pre_w;
        childParams.height = pre_h;
        this.getChildAt(0).setLayoutParams(childParams);
    }
}
